package com.myapp.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.gizwits.gizwifisdk.api.GizWifiSDK;
import com.gizwits.gizwifisdk.enumration.GizUserAccountType;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.listener.GizWifiSDKListener;
import com.myapp.R;
import com.myapp.utils.AppUtils;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity {
    private static final String TAG = "RegisterActivity";
    @BindView(R.id.tv_title_login)
    TextView tvTitleLogin;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_input_code)
    EditText etInputCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.et_input_password)
    EditText etInputPassword;
    @BindView(R.id.et_input_password_again)
    EditText etInputPasswordAgain;
    @BindView(R.id.iv_login_confirm)
    ImageView ivLoginConfirm;
    private String Phone;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        GizWifiSDK.sharedInstance().setListener(mListener);
    }

    @OnClick({R.id.iv_login_confirm,R.id.tv_title_login,R.id.tv_get_code})
    void onClick(View view){
        switch (view.getId()){
            case R.id.iv_login_confirm:
                Phone = etPhone.getText().toString();
                //检查phone格式
                if (!AppUtils.checkPhoneNum(Phone)){
                    Toast.makeText(getApplicationContext(),"手机号格式不对",Toast.LENGTH_SHORT).show();;
                    return;
                }

                String inputCode = etInputCode.getText().toString();
                String inputPassword = etInputPassword.getText().toString();
                String againPassword = etInputPassword.getText().toString();
                if(inputPassword.equals(againPassword)) {
                    // 调用SDK的邮箱号注册接口
                    GizWifiSDK.sharedInstance().registerUser(Phone, inputPassword, inputCode, GizUserAccountType.GizUserPhone);
                } else {
                    Toast.makeText(getApplicationContext(),"两次密码不同",Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.tv_title_login:
                finish();
            break;

            case R.id.tv_get_code:
                 sendVerifyCode();
                 break;
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
        timer = null;
    }

    // 实现回调
    GizWifiSDKListener mListener = new GizWifiSDKListener() {
        @Override
        public void didRegisterUser(GizWifiErrorCode result, String uid, String token) {
            if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
                Toast.makeText(getApplicationContext(),"注册成功",Toast.LENGTH_SHORT).show();
                // 注册成功
                Intent success = new Intent(getApplicationContext(),LoginActivity.class);
                startActivity(success);
            } else {
                Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_SHORT).show();
                // 注册失败
            }
        }

        @Override
        public void didRequestSendPhoneSMSCode(GizWifiErrorCode result, String token) {
            if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS){
                timer.start();
                Log.i(TAG, "didRequestSendPhoneSMSCode: "+"获取验证码成功");
            } else {
                Log.i(TAG, "didRequestSendPhoneSMSCode: "+"获取验证码失败");
                tvGetCode.setEnabled(true);
            }
        }
    };

    private void sendVerifyCode() {

        Phone = etPhone.getText().toString();
        //检查phone格式
        if (!AppUtils.checkPhoneNum(Phone)){
            Toast.makeText(getApplicationContext(),"手机号格式不对",Toast.LENGTH_SHORT).show();
            return;
        }

        String phone = etPhone.getText().toString();
        Log.i(TAG, "sendVerifyCode: " + "到达方法"+" phone:"+phone);
        GizWifiSDK.sharedInstance().requestSendPhoneSMSCode("4f5441985a3d4ac1802dd7ef92811b20",phone);
        tvGetCode.setEnabled(false);
    }

    private CountDownTimer timer = new CountDownTimer(60000,1000) {
        @Override
        public void onTick(long millisUntilFinished) {
                //doing something...
                String showTimeRemain = String.format(Locale.getDefault(),"%d秒后重发",(millisUntilFinished / 1000));
                tvGetCode.setText(showTimeRemain);
                tvGetCode.setEnabled(false);

        }
        @Override
        public void onFinish() {
                tvGetCode.setText("获得验证码");
                tvGetCode.setEnabled(true);

        }
    };

}
